package com.yt.db.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
public class DataSourceTransaction extends DataSourceTransactionManagerAutoConfiguration {

    @Resource
    private DataSource dataSource;

    protected Logger logger = LoggerFactory.getLogger(getClass());
    /**
     * 自定义事务
     * MyBatis自动参与到spring事务管理中，无需额外配置，只要org.mybatis.spring.SqlSessionFactoryBean引用的数据源与DataSourceTransactionManager引用的数据源一致即可，否则事务管理会不起作用。
     * @return DataSourceTransactionManager
     */
//    @Bean(name = "transactionManager")
//    @Primary
//    public DataSourceTransactionManager transactionManagers() {
//        logger.info("-------------------- transactionManager init ---------------------");
//        return new DataSourceTransactionManager(dataSource);
//    }
}